/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xmlutils;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;
import org.xml.sax.SAXException;

/**
 *
 * @author NoiR
 */
public class SongPDFGenerate {
    //Song

    private static final String xslt = "SongsFO.xsl";
    private static final String xml = "Songs.xml";
    private static final String FOrs = "SongsFO.fo";
    private static final String PDFrs = "SongsPDF.pdf";
    private static final String xsltDir = "web/xsl/";
    private static final String xmlDir = "web/xml/";

    public static void main(String[] args) {
        ExportPDF();
    }

    public static void ExportPDF() {
        // generate FO ->
        if (TrAXImplement()) {
            // use FO to generate PDF ->
            GeneratePDF();
        }
    }

    public static boolean TrAXImplement() {
        try {
            TransformerFactory tf = TransformerFactory.newInstance();
            StreamSource xsltFile = new StreamSource(xsltDir + xslt);
            Transformer trans = tf.newTransformer(xsltFile);

            StreamSource xmlFile = new StreamSource(xmlDir + xml);
            StreamResult pdfFile = new StreamResult(
                    new FileOutputStream(xsltDir + FOrs));
            trans.transform(xmlFile, pdfFile);

            return true;
        } catch (TransformerException ex) {
            System.out.println("Transformer Error: " + ex.getMessage());
            return false;
        } catch (FileNotFoundException ex) {
            System.out.println("File Not Found: " + ex.getMessage());
            return false;
        }
    }

    public static void GeneratePDF() {
        try {
            File xmlFile = new File(xsltDir + FOrs);
            File pdfFile = new File(xsltDir + PDFrs);
            FopFactory fopfac = FopFactory.newInstance();
            FOUserAgent foUserAgent = fopfac.newFOUserAgent();
            File config = new File(xsltDir + "myconfig.xconf");
            fopfac.setUserConfig(config);
            OutputStream output = new BufferedOutputStream(new FileOutputStream(pdfFile));
            Fop fop = fopfac.newFop(MimeConstants.MIME_PDF, foUserAgent, output);

            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer();
            Source src = new StreamSource(xmlFile);
            Result res = new SAXResult(fop.getDefaultHandler());

            transformer.transform(src, res);
        } catch (TransformerException ex) {
            System.out.println("Transform Error: "+ex.getMessage());
        } catch (SAXException ex) {
            Logger.getLogger(SongPDFGenerate.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(SongPDFGenerate.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}
